<?php
import( 'de.ceus-media.service.Service' );
import( 'de.ceus-media.database.StatementBuilder' );
import( 'classes.models.BugStatements' );
import( 'classes.models.Bug' );
import( 'classes.models.User' );
/**
 *	Main Services.
 *	@package		tools.BugTracker
 *	@extends		Service
 *	@uses			StatementBuilder
 *	@uses			BugStatements
 *	@uses			Bug
 *	@uses			User
 */
/**
 *	Main Services.
 *	@package		tools.BugTracker
 *	@extends		Service
 *	@uses			StatementBuilder
 *	@uses			BugStatements
 *	@uses			Bug
 *	@uses			User
 */
class PublicServices extends Service
{
	/**
	 *	Constructor.
	 *	@access		public
	 *	@return		void
	 */
	public function __construct()
	{
		$this->ref	= new ADT_Reference;
		$config		= $this->ref->get( 'config' );
		$this->rss	= $config['rss'];
	}
	
	/**
	 *	Returns Count of all Articles.
	 *	@access		public
	 *	@return		string
	 */
	public function getTotalBugCount( $format, $parameters )
	{
		$bug	= new Bug();
		$count	= $bug->getAllCount();
		if( $format == "xml" )
		{
			$root	=& new XML_DOM_Node( 'bugs' );
			$node	=& new XML_DOM_Node( 'count', $count );
			$root->addChild( $node );
			$xb	= new XML_DOM_Builder();
			header('Content-Type: text/xml');
			echo $xb->build( $root );
		}
		else if( $format == "json" )
			echo $this->getJson( $count );
		else if( $format == "php" )
			echo $this->getPhp( $count);
		else if( $format== "txt" )
			echo $count;
	}

	/**
	 *	Returns Count of all Articles.
	 *	@access		public
	 *	@return		string
	 */
	public function getOpenBugsForUser( $format, $parameters )
	{
		$dbc		= $this->ref->get( "dbc" );
		$config		= $this->ref->get( "config" );
		$language	= $this->ref->get( 'language' );
		$language->loadLanguage( 'bug' );
		
		$user_id	= $parameters['user_id'];
		$conditions	= array(
			'to_user_id'	=> $user_id,
			'status'		=> "<=1",
			);

		$sb		= new StatementBuilder( $config['config']['table_prefix'] );
		$bs		= new BugStatements( $sb );
		$bs->addComponent( 'Base' );
		$bs->addComponent( 'Developer', $user_id );
		$bs->addComponent( 'Status', array( 1, 3 ) );
		$query	= $sb->buildSelectStatement();
		$result	= $dbc->execute( $query );
		$count	= $result->recordCount();
	
		if( $format == "xml" )
		{
			$root	=& new XML_DOM_Node( 'bugs' );
			while( $entry	= $result->fetchNextArray( false ) )
			{
				$node	=& new XML_DOM_Node( 'bug' );
				$child	=& new XML_DOM_Node( 'title', $entry['title'] );
				$node->addChild( $child );
				$child	=& new XML_DOM_Node( 'server', $entry['server'] );
				$node->addChild( $child );
				$child	=& new XML_DOM_Node( 'project', $entry['project'] );
				$node->addChild( $child );
				$child	=& new XML_DOM_Node( 'status', $language->words['main']['states'][$entry['status']] );
				$node->addChild( $child );
				$child	=& new XML_DOM_Node( 'priority', $language->words['main']['priorities'][$entry['priority']] );
				$node->addChild( $child );
				$root->addChild( $node );
			}
			$xb	= new XML_DOM_Builder();
			header('Content-Type: text/xml');
			echo $xb->build( $root );
		}
		else if( $format == "json" )
			echo $this->getJson( $data );
		else if( $format == "php" )
			echo $this->getPhp( $data);
	}
}
?>